﻿using System;

using DevExpress.Xpo;

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;

namespace SpeedSCM.Module
{
    [NavigationItem("库存管理")]
    [ImageName("BO_Product_Group")]
    public class 商品拆装单 : 单据
    {
        public 商品拆装单(Session session)
            : base(session)
        {

        }
        protected override SeedType SeedType
        {
            get { return SeedType.商品拆装单; }
        }

        private 仓库 _出货仓库;
        [Custom("Caption", "出货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择出货仓库")]
        public 仓库 出货仓库
        {
            get { return _出货仓库; }
            set { SetPropertyValue<仓库>("出货仓库", ref _出货仓库, value); }
        }
        private 仓库 _入货仓库;
        [Custom("Caption", "入货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择入货仓库")]
        public 仓库 入货仓库
        {
            get { return _入货仓库; }
            set { SetPropertyValue<仓库>("入货仓库", ref _入货仓库, value); }
        }

        [Association("商品拆装单-拆装出库明细")]
        public XPCollection<商品拆装单出库明细> 商品拆装单出库明细
        {
            get { return GetCollection<商品拆装单出库明细>("商品拆装单出库明细"); }
        }
        [Association("商品拆装单-拆装入库明细")]
        public XPCollection<商品拆装单入库明细> 商品拆装单入库明细
        {
            get { return GetCollection<商品拆装单入库明细>("商品拆装单入库明细"); }
        }
        public override bool Excute(IObjectSpace objectSpace)
        {
            foreach (商品拆装单出库明细 item in 商品拆装单出库明细)
            {
                //出货仓库
                库存 s1 = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 出货仓库));
                if (s1 != null)
                {
                    s1.库存数量 -= item.出库数量;
                    s1.Save();
                }
            }
            foreach (商品拆装单入库明细 item in 商品拆装单入库明细)
            {
                //入货仓库
                库存 s2 = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 入货仓库));
                if (s2 == null)
                {
                    s2 = objectSpace.CreateObject<库存>();
                    s2.商品名称 = item.商品名称;
                    s2.仓库名称 = 入货仓库;
                    s2.库存数量 = item.入库数量;
                }
                else
                {
                    s2.库存数量 += item.入库数量;
                }
                s2.Save();
            }
            objectSpace.CommitChanges();
            return base.Excute(objectSpace);
        }
    }

    [ImageName("BO_Product_Group")]
    [RuleCombinationOfPropertiesIsUnique(null, DefaultContexts.Save, "商品拆装单,商品名称", CustomMessageTemplate = "商品拆装单中已经存在该商品")]
    public class 商品拆装单出库明细 : BaseObject
    {
        public 商品拆装单出库明细(Session session)
            : base(session)
        {

        }
        private 商品拆装单 _商品拆装单;
        [Custom("Caption", "商品拆装单")]
        [Association("商品拆装单-拆装出库明细")]
        [Custom("AllowEdit", "False")]
        public 商品拆装单 商品拆装单
        {
            get { return _商品拆装单; }
            set { SetPropertyValue<商品拆装单>("商品拆装单", ref _商品拆装单, value); }
        }
        private 商品 _商品名称;
        [Custom("Caption", "商品名称")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择商品")]
        public 商品 商品名称
        {
            get { return _商品名称; }
            set
            {
                if (SetPropertyValue<商品>("商品名称", ref _商品名称, value))
                {
                    成本单价 = value.进货价;
                }
            }
        }
        private decimal _出库数量;
        [Custom("Caption", "出库数量")]
        [Custom("EditMask", "n2")]
        [Custom("DisplayFormat", "{0:n2}")]
        [RuleRequiredField]
        public decimal 出库数量
        {
            get { return _出库数量; }
            set { SetPropertyValue<decimal>("出库数量", ref _出库数量, value); }
        }
        private decimal _成本单价;
        [Custom("Caption", "成本单价")]
        [RuleRequiredField]
        public decimal 成本单价
        {
            get { return _成本单价; }
            set { SetPropertyValue<decimal>("成本单价", ref _成本单价, value); }
        }
        public decimal 成本金额
        {
            get { return 出库数量 * 成本单价; }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        [Size(500)]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }
    }
    [ImageName("BO_Product_Group")]
    [RuleCombinationOfPropertiesIsUnique(null, DefaultContexts.Save, "商品拆装单,商品名称", CustomMessageTemplate = "商品拆装单中已经存在该商品")]
    public class 商品拆装单入库明细 : BaseObject
    {
        public 商品拆装单入库明细(Session session)
            : base(session)
        {

        }
        private 商品拆装单 _商品拆装单;
        [Custom("Caption", "商品拆装单")]
        [Association("商品拆装单-拆装入库明细")]
        [Custom("AllowEdit", "False")]
        public 商品拆装单 商品拆装单
        {
            get { return _商品拆装单; }
            set { SetPropertyValue<商品拆装单>("商品拆装单", ref _商品拆装单, value); }
        }
        private 商品 _商品名称;
        [Custom("Caption", "商品名称")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择商品")]
        public 商品 商品名称
        {
            get { return _商品名称; }
            set
            {
                if (SetPropertyValue<商品>("商品名称", ref _商品名称, value))
                {
                    成本单价 = value.进货价;
                }
            }
        }
        private decimal _入库数量;
        [Custom("Caption", "入库数量")]
        [Custom("EditMask", "n2")]
        [Custom("DisplayFormat", "{0:n2}")]
        [RuleRequiredField]
        public decimal 入库数量
        {
            get { return _入库数量; }
            set { SetPropertyValue<decimal>("入库数量", ref _入库数量, value); }
        }

        private decimal _成本单价;
        [Custom("Caption", "成本单价")]
        [RuleRequiredField]
        public decimal 成本单价
        {
            get { return _成本单价; }
            set { SetPropertyValue<decimal>("成本单价", ref _成本单价, value); }
        }
        public decimal 成本金额
        {
            get { return 入库数量 * 成本单价; }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        [Size(500)]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }
    }
}
